package dao;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import entity.Commissioned;
import entity.SalesCard;

@Stateless
public class SalesCardDao {
	@PersistenceContext
	EntityManager manager;

	// insert a sales card
	public SalesCard CreateSalesCard(Date date, float amount,
			Commissioned employee) {
		SalesCard card = new SalesCard(date, amount, employee);
		manager.persist(card);
		return card;
	}

	// finds a sales card of an employee
	public List<SalesCard> listSales(Commissioned employee) {
		String spql = "Select s from SalesCard s where s.commissioned='" + employee.getId() + "'";
		TypedQuery<SalesCard> query = manager.createQuery(spql,SalesCard.class);
		List<SalesCard> listSales = query.getResultList();
		return listSales;
	}
	public void LoadSalesCard(Commissioned employee) {
		String spql = "Select s from SalesCard s where s.commissioned='" + employee.getId() + "'";
		TypedQuery<SalesCard> query = manager.createQuery(spql,SalesCard.class);
		employee.setSalesCards(query.getResultList());
	}

	// finds a specific sales card
	public SalesCard findCard(int id) {
		SalesCard card = manager.find(SalesCard.class, id);
		return card;
	}

	// delete a sales card
	public int deleteSalesCard(SalesCard card) {
		try {
			SalesCard deleteCard=manager.merge(card);
			manager.remove(deleteCard);
			return 0;
		} catch (Exception e) {
			return -1;
		}
	}
}
